package com.im.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.im.pojo.Invitation;
import com.im.service.InvitationService;

/**
 * @author Johnny
 * @category 帖子的控制器
 */
@Controller
public class InvitationController {

	@Resource
	private InvitationService invitationService;

	Logger log = Logger.getLogger(InvitationController.class);

	@RequestMapping({ "/index", "/" })
	public String index() {
		log.debug("index...");
		return "forward:/index.jsp";
	}

	@RequestMapping("/list")
	public String list(Model model) {
		log.debug("list....");
		List<Invitation> list = invitationService.find();
		model.addAttribute("list", list);
		log.debug("list sice " + list.size());
		return "list";
	}

	@RequestMapping("/add")
	public String add() {
		log.debug("to add jsp....");
		return "add";
	}

	@RequestMapping("/addsave")
	public String addSave(Invitation invitation) {
		log.debug("invitation.title: " + invitation.getTitle());
		boolean flag = invitationService.insert(invitation);
		log.debug("flag:" + flag);
		return "redirect:list.action";
	}
	
	@RequestMapping("/delete")
	public String delete(int[] ids){
		boolean flag = invitationService.delete(ids);
		log.debug("flag:" + flag);
		return "redirect:list.action";
	}
	
	//先跳转到更新页面
	@RequestMapping("/update")
	public String update(int id,Model model){
		Invitation invitation = invitationService.find(id);
		model.addAttribute("invitation", invitation);
		return "update";
	}
	
	//将更新的信息传递进来
	@RequestMapping("/updatesave")
	public String updateSave(Invitation invitation){
		boolean flag = invitationService.update(invitation);
		log.debug("flag:" + flag);
		return "redirect:list.action";
	}
	
	@RequestMapping("/findbyid")
	public String findById(int id,Model model){
		Invitation invitation = invitationService.find(id);
		model.addAttribute("invitation", invitation);
		log.debug("invitation:" + invitation);
		return "info";
	}
}